package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.UserMessageRoleMapper;
import com.jlkf.pojo.User;
import com.jlkf.pojo.UserMessageRole;
import com.jlkf.service.UserMessageRoleService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.RegexUtil;
import com.platform.server.util.StringTools;
@Service
public class UserMessageRoleServiceImpl implements UserMessageRoleService{

	@Autowired
	private UserMessageRoleMapper userMessageRoleMapper;
	
	@Override
	public Object getList(String umRoleName, Integer pageNo, Integer pageSize) {
		Map<String,Object> map = new HashMap<>();
	    if(!StringTools.isEmpty(umRoleName)){
	    	map.put("umRoleName", umRoleName);
	    }
	    if(RegexUtil.isNull(pageNo)){
	    	pageNo=1;
	    }
	    if(RegexUtil.isNull(pageSize)){
	    	pageSize=10;
	    }
	    try {
	    	PageHelper.startPage(pageNo, pageSize);
			List<UserMessageRole> list = userMessageRoleMapper.getList(map);
			PageInfo<UserMessageRole> info = new PageInfo(list);
			Integer totalPage= info.getPages();
			Integer totalRecord = (int) info.getTotal();
			return new ReturnData(list,totalPage,totalRecord,200,"User list ");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object addOrUpdate(String json) {
		UserMessageRole umr = StringTools.json2Obj(json, UserMessageRole.class);
		try {
			if(umr.getId()!=null){
				int num=userMessageRoleMapper.updateByPrimaryKeySelective(umr);
				if(num==0){
					return new ReturnData(201,"更新失败");
				}
			}else{
				umr.setCreateTime(new Date());
				int num=userMessageRoleMapper.insertSelective(umr);
				if(num==0){
					return new ReturnData(201,"添加失败");
				}
			}
			return new ReturnData(200,"添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	@Override
	public Object del(String ids) {
		try {
			String[] strlist=ids.split(",");
			for(String str:strlist){
				int num=userMessageRoleMapper.deleteByPrimaryKey(Integer.parseInt(str));
				if(num==0){
					return new ReturnData(201,"删除失败");
				}
			}
			return new ReturnData(200,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

}
